<template>
  <div v-loading="allLoading">
    <el-form ref="queryForm" :model="queryParams" :inline="true" @submit.prevent.native>
      <el-form-item :label="$t('公司名称')" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          :placeholder="$t('公司名称')"
          clearable
          :onsubmit="false"
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>

      <el-table ref="multipleTable" v-loading="loading" :data="companyList" :row-key="getRowKey" border @selection-change="getCheckedData">
        <el-table-column
          v-model="companySelIdList"
          type="selection"
          :reserve-selection="true"
          width="55"
        />
        <el-table-column :label="$t('公司全称')" prop="companyName" :show-overflow-tooltip="true" fixed="left" width="250" />
        <el-table-column :label="$t('法人代表')" align="center" prop="legalPerson" show-overflow-tooltip />
        <el-table-column :label="$t('一级行业')" align="center" prop="industryFirstLabel" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ $t(scope.row.industryFirstLabel) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('二级行业')" align="center" prop="industrySecondLabel" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ $t(scope.row.industrySecondLabel) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('注册资本')+ $t('金额单位')" align="center" show-overflow-tooltip prop="capitalRegistered" width="110">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.capitalRegistered, 'w') }}</span>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <el-button @click="cancel">{{ $t("取消") }}</el-button>
      <el-button v-hasPermi="['deal:pcwMedical:edit']" type="primary" @click="submitForm">{{ $t("保存") }}</el-button>
    </div>
  </div>

</template>

<script>
import { findSelCompanyId, saveDealPcwCompanyContrast } from '@/api/deal/pcwmedical/dealPcwCompanyContrast'
import { listPcwCompany } from '@/api/company/base/companyBase'
export default {
  props: {
    companyId: {
      type: String,
      required: true,
      default: ''
    }
  },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 全页面遮罩层
          allLoading: false,
          // 全部公司是数据
          companyList: [],
          // 总条数
          total: 0,
          // 选中的公司ID
          companySelIdList: [],
          // 查询参数
          queryParams: {
              companyName: ''
          },
          contrastQueryParams: {
            companyId: this.companyId
          },
          // 表单校验
          rules: {},
          defaultProps: {
            children: 'children',
            label: 'companyName'
          },
          // 全选
          isAllChecked: false,
          form: {
            companyId: this.companyId
          }
      }
  },
  watch: {
    companySelIdList() {
      if (this.companySelIdList !== null && this.companySelIdList.length === this.companyList.length) {
        this.isAllChecked = true
      } else {
        this.isAllChecked = false
      }
    }
  },
  created() {
    this.getSelCompanyId()
    this.getList()
  },
  methods: {
    /** 查询关注科目列表 */
    getList() {
      this.companySelIdList = []
      this.form.contrastId = ''
      this.loading = true
      listPcwCompany(this.queryParams).then(response => {
        this.companyList = response.rows
        this.total = response.total
        findSelCompanyId(this.contrastQueryParams).then(response => {
          if (response.data !== null && response.data.contrastId !== null) {
            this.form.contrastId = response.data.contrastId
            response.data.contrastId.split(',').forEach(item => {
              this.companySelIdList.push(item)
            })
          } else {
            this.companySelIdList = []
          }
          if (this.form.contrastId.indexOf(this.companyId) < 0) {
            this.form.contrastId = this.companyId + ',' + this.form.contrastId
            this.companySelIdList.push(this.companyId)
          }
          // 回显被选中的公司
          this.companySelIdList.forEach(sel => {
            let idx = 0
            this.companyList.forEach(cmy => {
              if (sel === cmy.id) {
                this.$refs.multipleTable.toggleRowSelection(this.companyList[idx], true)
              }
              idx++
            })
          })
        })
      })
      this.loading = false
    },
    // 获取上一次选择的项目ID
    getSelCompanyId() {
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      this.$parent.$parent.formOpen = false
      // 刷新list页面
      this.$parent.$parent.getList()
    },
    // 选中事件
    getCheckedData(val) {
      if (val !== null && val !== undefined) {
        this.companySelIdList = []
        this.form.contrastId = ''
        val.forEach(item => {
          if (item.id !== this.companyId) {
            this.companySelIdList.push(item.id)
            this.form.contrastId = this.form.contrastId + ',' + item.id
          }
        })
        if (this.form.contrastId !== '') {
          this.form.contrastId = this.form.contrastId.substring(1)
        }
        if (this.form.contrastId.indexOf(this.companyId) < 0) {
          const ctid = this.companyId + ',' + this.form.contrastId
          this.form.contrastId = ctid
          this.companySelIdList.push(this.companyId)
        }
      }
    },
    // 获取rowKey
    getRowKey(row) {
      return row.id
    },
    /** 提交按钮 */
    submitForm: function() {
      this.allLoading = true
      saveDealPcwCompanyContrast(this.form).then(response => {
        if (response.code === 200) {
          this.msgSuccess(this.$t('保存成功'))
          this.$parent.$parent.initVSData(this.form.contrastId)
          this.closeForm()
        } else {
          this.msgError(response.msg)
        }
        this.allLoading = false
      })
    }
  }
}
</script>
